package com.youlai.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.SalaryForm;
import com.youlai.system.model.query.SalaryQuery;
import com.youlai.system.model.vo.SalaryVO;
import com.youlai.system.model.vo.SalaryVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SalaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

@Tag(name = "15.工资接口")
@RestController
@RequestMapping("/api/v1/salary")
@RequiredArgsConstructor
public class SalaryController {
    private final SalaryService salaryService;
    @Operation(summary = "工资全部信息分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/list")
    public PageResult<SalaryVO> getSalaryPageAll(
            @ParameterObject SalaryQuery queryParams
    ){

        PageInfo<SalaryVO> pageInfo = salaryService.getSalaryList(queryParams);

        // 将PageInfo对象转换为IPage对象
        Page<SalaryVO> iPage = new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
        iPage.setRecords(pageInfo.getList());
        iPage.setTotal(pageInfo.getTotal());

        return PageResult.success(iPage);
    }
    @Operation(summary = "修改工资")
    @PostMapping("/update")
    @PreventDuplicateSubmit
    public Result updateSalary(
            @Valid @RequestBody SalaryForm salaryForm
    ) {
        Integer id = salaryService.UpdateSalary(salaryForm);
        return Result.success(id);
    }
    @Operation(summary = "获取社保信息")
    @GetMapping("/{id}/VO")
    public Result<SalaryVO> selectSalaryAllById(
            @Parameter(description = "社保id") @PathVariable Integer id) {
        SalaryVO salaryVO = salaryService.selectById(id);
        return Result.success(salaryVO);
    }
    @Operation(summary = "新增工资")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public Result addSalary(
            @Valid @RequestBody SalaryForm salaryForm
    ) {
        boolean result= salaryService.addSalary(salaryForm);
        return Result.success(result);
    }
}
